package com.example.common.component;


import com.example.common.model.RsaApiCreateResp;
import com.example.common.model.UserApiGetUserFavorResp;
import com.example.common.model.UserApiModifyInformationResp;
import com.example.common.network.RetrofitServiceManager;
import com.example.common.network.UserReq;

import com.example.common.control.ICallback;
import com.example.common.model.Description;
import com.example.common.model.MyResponse;

import java.text.SimpleDateFormat;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class UserCtrlCom {
    private UserReq userReq;
    public UserCtrlCom(){
        userReq = RetrofitServiceManager.getInstance().create(UserReq.class);
    }


    /**
     * 获取用户信息
     * @param token
     * @param iCallback 回调，返回类型 Description
     */
    public void getUser(String token, ICallback iCallback) {
        //创建请求
        Call<MyResponse<Description>> call = userReq.getUser(token);

        //发出请求
        call.enqueue(new Callback<MyResponse<Description>>() {
            @Override
            public void onResponse(Call<MyResponse<Description>> call, Response<MyResponse<Description>> response) {
                if(response.isSuccessful()){
                    MyResponse<Description> myResponse=response.body();
                    if(myResponse!=null){
                        if(myResponse.getCode()==200){
                            iCallback.onSuccess(myResponse.getData());
                        }else {
                            iCallback.onFailed(myResponse.getErrmsg());
                        }
                    }else {
                        iCallback.onFailed("请求体为空");
                    }
                }else {
                    iCallback.onFailed("请求错误");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<Description>> call, Throwable t) {

            }
        });
    }

    /**
     * 修改用户资料——响应时需要修改本地TOKEN
     * @param token 用户TOKEN
     * @param avatar 头像地址
     * @param birthday 生日
     * @param gender 性别：1,0,2
     * @param introduction 简介
     * @param username 用户名
     * @param iCallback 回调
     */
    public void putUser(String token, String avatar, String birthday, Integer gender, String introduction, String username, ICallback iCallback){
        Call<MyResponse<UserApiModifyInformationResp>> call=userReq.putUser(token,avatar,birthday,gender,introduction,username);
        call.enqueue(new Callback<MyResponse<UserApiModifyInformationResp>>() {
            @Override
            public void onResponse(Call<MyResponse<UserApiModifyInformationResp>> call, Response<MyResponse<UserApiModifyInformationResp>> response) {
                if(response.isSuccessful()){
                    MyResponse<UserApiModifyInformationResp> myResponse= response.body();
                    if(myResponse!=null){
                        if(myResponse.getCode()==200){
                            //返回类型 UserApiModifyInformationResp
                            iCallback.onSuccess(myResponse.getData());
                        }else {
                            iCallback.onFailed(myResponse.getErrmsg());
                        }
                    }else {
                        iCallback.onFailed("响应为空");
                    }
                }else {
                    iCallback.onFailed("请求错误");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<UserApiModifyInformationResp>> call, Throwable t) {
                    iCallback.onFailed("网络错误");
            }
        });
    }

    /**
     * 获取用户个人收藏游戏
     * @param token 用户token，从GlobalUser取
     * @param id 用户id，从GlobalUser取
     * @param iCallback 回调——onSuccess返回类型UserApiGetUserFavorResp
     */
    public void getUserIdFavor(String token,Integer id,ICallback iCallback){
        Call<MyResponse<UserApiGetUserFavorResp>> call=userReq.getUserIdFavor(id);
        call.enqueue(new Callback<MyResponse<UserApiGetUserFavorResp>>() {
            @Override
            public void onResponse(Call<MyResponse<UserApiGetUserFavorResp>> call, Response<MyResponse<UserApiGetUserFavorResp>> response) {
                MyResponse<UserApiGetUserFavorResp> myResponse=response.body();
                if(myResponse!=null){
                    if(myResponse.getCode()==200){
                        iCallback.onSuccess(myResponse.getData());
                    }else {
                        iCallback.onFailed(myResponse.getErrmsg());
                    }
                }else {
                    iCallback.onFailed("响应体为空");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<UserApiGetUserFavorResp>> call, Throwable t) {
                call.cancel();
                iCallback.onFailed(t.getMessage());
            }
        });
    }
}
